package com.example.demo.controller;

import com.example.demo.entity.Dept;
import com.example.demo.entity.Result;
import com.example.demo.service.Impl.DeptServiceImpl;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
//    private DeptServiceImpl deptService = new DeptServiceImpl();
    @Autowired
    private DeptServiceImpl deptService;
//    @RequestMapping(value = "depts",method =  RequestMethod.GET)

    /**
     * 查询所有depts
     * @return
     */
    @GetMapping
    public Result list(){
        List<Dept> deptList = deptService.queryDeptList();
//        System.out.println(Result.success(deptList));
        return Result.success(deptList);

    }

    /**
     * 删除指定 dept
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id){
        log.info("id值："+id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增 指定的 dept
     */
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("添加dept:"+dept);
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据id查询dept
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("dept的id值："+id);
        Dept dept =  deptService.getInfo(id);
        return Result.success(dept);
    }
    /**
     * 修改dept
     */
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改的dept:"+dept);
        deptService.update(dept);
        return Result.success();
    }
}
